using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace OperationInfo{
    public partial class OperationForm : Form{
        public OperationForm(){
            InitializeComponent();
        }
        //编写数据库连接串
        string connStr = "Data Source= CZBK-20190302ZQ\\MSSQLSERVER2012;
Initial Catalog=School;User ID=sa;Password=123456";
        private void btnAdd_Click(object sender, EventArgs e){
            try{
                using (SqlConnection conn = new SqlConnection(connStr)){
                    conn.Open(); //打开数据库连接              
                    if (tbName.Text == null || tbName.Text.Length == 0 || 
tbName.Text == " "){//判断输入的数据不能为空
                        MessageBox.Show("请输入姓名！");
                        return;
                    }
                    if (tbAge.Text == null || tbAge.Text.Length == 0 || tbAge.Text == " ")
{//判断输入的数据不能为空
                        MessageBox.Show("请输入年龄！");
                        return;
                    }
                    if (cbSex.Text == null || cbSex.Text.Length == 0 || cbSex.Text == " ")
{//判断输入的数据不能为空
                        MessageBox.Show("请选择性别！");
                        return;
                    }
                    string selectsql = "insert into Student values('" + tbName.Text + "','"
 + tbAge.Text + "','" + cbSex.Text + "')";
                    SqlCommand cmd = new SqlCommand(selectsql, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("添加成功！");
                    DataBind();
                }
            }catch (Exception ex){
                MessageBox.Show("添加失败！" + ex.Message);
            }
        }
   private void btnQuery_Click(object sender, EventArgs e){
            DataBind();
        }
        private void DataBind(){
            try{
                using (SqlConnection conn = new SqlConnection(connStr)){
                    conn.Open(); //打开数据库连接
                    SqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "select Id 编号,Name 姓名,Age 年龄,Sex 性别 from
 Student";
                    SqlDataAdapter sda = new SqlDataAdapter(comm);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    dgvStuden  
                    private void btnAlter_Click(object sender, EventArgs e){
            try{
                using (SqlConnection conn = new SqlConnection(connStr)){
                    conn.Open(); //打开数据库连接
                    string updatesql = "update student set Name = '" + tbName.Text + "',Age='"
                       + tbAge.Text + "', Sex='" + cbSex.Text + "'where Id = '" 
+ tbId.Text + "'";
                    SqlCommand cmd = new SqlCommand(updatesql, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("修改成功！");
                    DataBind();
                }
            }catch (Exception ex){
                MessageBox.Show("修改失败！" + ex.Message);
            }
        }    
         private void btnDelete_Click(object sender, EventArgs e){
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "确定", messButton);
            if (dr == DialogResult.OK){
                 try{
                     using (SqlConnection conn = new SqlConnection(connStr)){
                         conn.Open(); //打开数据库连接
                         SqlCommand comm = conn.CreateCommand();
                         comm.CommandText = "delete from Student where Id = @Id";
                         comm.Parameters.AddWithValue("@Id", tbId.Text);
                         comm.ExecuteNonQuery();
                         MessageBox.Show("删除成功！");
                         DataBind();
                     }
                 }catch (Exception ex){
                     MessageBox.Show("删除失败！" + ex.Message);
                 }
            }
        }
    }
}